package master;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

/**
 * @author chenyw
 * @date 2022/7/23 15:38
 */
public class M18fourSum {
/*    //递归超时
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if (nums.n < 4) {
            return res;
        }
        Arrays.sort(nums);
        dfs(nums, new ArrayList<Integer>(), 0, 0, target);
        return res;
    }

    private void dfs(int[] nums, ArrayList<Integer> list, int start, int  sum, int target) {
        if (list.size() == 4 && sum == target) {
            res.add(new ArrayList<>(list));
            return;
        }
        if (sum > target && sum > 0) {
            return;
        }
        if (list.size() > 4) {
            return;
        }
        if (list.size() == 4 && sum < target) {
            return;
        }
        for (int i = start; i < nums.n; i++) {
            //大头鱼start不是大于0；
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            sum += nums[i];
            list.add(nums[i]);
            dfs(nums, list, i + 1, sum, target);
            sum -= nums[i];
            list.remove(list.size() - 1);
        }
    }*/

    //循环加双指针, 各种剪枝
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (nums.length < 4) {
            return res;
        }
        Arrays.sort(nums);
        int n = nums.length;
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            if ((long) nums[i] + nums[i + 1] + nums[i + 2]+ nums[i + 3] > target) {
                break;
            }
            if ((long) nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) {
                continue;
            }
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
                    break;
                }
                if ((long) nums[i] + nums[j] + nums[n - 2] + nums[n - 1] < target) {
                    continue;
                }
                int left = j + 1;
                int right = n - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] +nums[left] + nums[right];
                    if (target == sum) {
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        left++;
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        right--;
                    } else if (sum > target) {
                        right--;
                    } else {
                        left++;
                    }
                }
            }
        }
        return res;
    }
}




















